<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pastephoto extends CI_Controller {
	public function index()
	{
		$posts = $this->input->post();
		if ( isset($posts) && $posts['castit_image_data'] != '' )
		{
			$session_id = $this->session->userdata('session_id');
			$image_data = $posts['castit_image_data'];
			$filteredData = substr($image_data, strpos($image_data, ",")+1);
			$unencodedData = base64_decode($filteredData);
			$filename = uniqid().'.png';
			$fp = fopen('./content/uploads/'.$filename, 'wb');
			fwrite($fp, $unencodedData);
			fclose($fp);
			
			
			$insert_data = array('session_id'=>$session_id, 'photo_file'=>$filename);
			$this->db->insert('photos', $insert_data);

			$this->db->where('session_id', $session_id);
			$query = $this->db->get('photos');
			$data = array();
			if ( $query->num_rows() > 0 )
			{
				$result_array = $query->result_array();
				$data['photos'] = $result_array;
				$this->load->view('header');
				$this->load->view('pastephoto',$data);
				$this->load->view('footer');
			}
			else
			{
				redirect(site_url('personalize'));
			}
		}
		else
		{
			$session_id = $this->session->userdata('session_id');
			$this->db->where('session_id', $session_id);
			$query = $this->db->get('photos');
			$data = array();
			if ( $query->num_rows() > 0 )
			{
				$result_array = $query->result_array();
				$data['photos'] = $result_array;
				$this->load->view('header');
				$this->load->view('pastephoto',$data);
				$this->load->view('footer');
			}
			else
			{
				redirect(site_url('personalize'));
			}
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */